package cn.mothin.service.system;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cn.mothin.dao.SystemConfigDetailDao;
import cn.mothin.dao.SystemConfigTypeDao;
import cn.mothin.entity.SystemConfigDetail;
import cn.mothin.entity.SystemConfigType;
import cn.mothin.frame.bean.ActionResult;
import cn.mothin.frame.core.base.BaseService;
import cn.mothin.frame.help.UniqueCode;
import cn.mothin.frame.util.StringUtil;
import cn.mothin.vo.system.ConfigForm;

/**
 * 
 * @author 张权
 */
@Service("540ebca8b2effe522cf1605f")
public class ConfigService extends BaseService {

    @Autowired
    private SystemConfigTypeDao systemConfigTypeDao;

    @Autowired
    private SystemConfigDetailDao systemConfigDetailDao;

    public String query(ConfigForm configForm) {
        String sql = "SELECT * FROM system_config_type WHERE allowConfig <> true";
        return sqlHelper.queryJson(configForm, sql);
    }

    /**
     * 查询系统配置类型
     * 
     * @param configForm
     */
    public void queryConfigType(ConfigForm configForm) {
        configForm.setSystemConfigType(systemConfigTypeDao.get(configForm
                .getSystemConfigType().getId()));
    }

    /**
     * 添加配置类型
     * 
     * @param configForm
     * @return
     */
    public ActionResult addSystemConfigType(ConfigForm configForm) {
        ActionResult result = ActionResult.getInstance();

        if (systemConfigTypeDao.getCountByName(configForm.getSystemConfigType()
                .getName()) > 0) {
            result.setOk(false);
            result.setMessage("相同配置名称类型已存在");
        } else {
            String id = UniqueCode.make();
            SystemConfigType systemConfigType = new SystemConfigType();
            systemConfigType.setId(id);
            systemConfigType
                    .setName(configForm.getSystemConfigType().getName());
            systemConfigType.setAllowConfig(configForm.getSystemConfigType()
                    .isAllowConfig());
            systemConfigTypeDao.save(systemConfigType);

            result.setValue(id);
        }
        return result;
    }

    /**
     * 修改系统配置类型
     * 
     * @param configForm
     * @return
     */
    public ActionResult updateSystemConfigType(ConfigForm configForm) {
        ActionResult result = ActionResult.getInstance();
        SystemConfigType systemConfigType = systemConfigTypeDao.get(configForm
                .getSystemConfigType().getId());

        if (systemConfigType == null) {
            result.setOk(false);
            result.setMessage("此配置不存在");

        } else if (systemConfigTypeDao.getCountByName(configForm
                .getSystemConfigType().getName()) > 0) {
            result.setOk(false);
            result.setMessage("相同配置名称类型已存在");
        } else {
            String name = configForm.getSystemConfigType().getName();
            boolean allowConfig = configForm.getSystemConfigType()
                    .isAllowConfig();
            systemConfigType.setName(name);
            systemConfigType.setAllowConfig(allowConfig);
            systemConfigTypeDao.update(systemConfigType);

            result.setValue(systemConfigType.getId());

        }
        return result;
    }

    /**
     * 返回要更改的配置项信息
     * 
     * @param configForm
     */
    public int queryTypeById(ConfigForm configForm) {
        String id = configForm.getSystemConfigType().getId();
        if (StringUtil.isNullOrEmpty(id)) {
            return 1;
        }
        SystemConfigType systemConfigType = systemConfigTypeDao.get(id);
        if (systemConfigType == null) {
            return 1;
        }
        configForm.setSystemConfigType(systemConfigType);
        return 0;

    }

    /**
     * 删除系统配置类型
     * 
     * @param configForm
     * @return
     */
    public ActionResult deleteSystemConfigType(ConfigForm configForm) {
        ActionResult result = ActionResult.getInstance();
        String id = configForm.getSystemConfigType().getId();
        if (systemConfigTypeDao.getCountById(id) > 0) {
            result.setOk(false);
            result.setMessage("此配置类型下还有配置详细，不允许删除!");
        }
        SystemConfigType systemConfigType = systemConfigTypeDao.get(id);
        if (systemConfigType == null) {
            result.setOk(false);
            result.setMessage("此配置已不存在");
        } else {
            systemConfigTypeDao.delete(systemConfigType);
            // result.setValue(id);
        }
        return result;
    }

    /**
     * 查询系统配置详细
     * 
     * @param configForm
     * @return
     */
    public String queryConfigDetail(ConfigForm configForm) {
        String sql = "SELECT id, name, value, memo FROM system_config_detail WHERE allowConfig <> true AND systemConfigTypeId= ? ";
        return systemConfigDetailDao.queryJson(configForm, sql, configForm
                .getSystemConfigType().getId());

    }

    /**
     * 保存添加的系统配置详细
     * 
     * @param configForm
     * @return
     */
    public ActionResult addSystemConfigDetail(ConfigForm configForm) {
        ActionResult result = ActionResult.getInstance();

        if (systemConfigDetailDao.getCountByName(configForm
                .getSystemConfigDetail().getName()) > 0) {
            result.setOk(false);
            result.setMessage("相同配置详细类型已存在");
        } else {
            String id = UniqueCode.make();
            SystemConfigDetail systemConfigDetail = new SystemConfigDetail();
            systemConfigDetail.setId(id);
            systemConfigDetail.setName(configForm.getSystemConfigDetail()
                    .getName());
            systemConfigDetail.setValue(configForm.getSystemConfigDetail()
                    .getValue());
            systemConfigDetail.setMemo(configForm.getSystemConfigDetail()
                    .getMemo());
            systemConfigDetail.setSystemConfigTypeId(configForm
                    .getSystemConfigType().getId());
            systemConfigDetailDao.save(systemConfigDetail);

            result.setValue(id);
        }
        return result;
    }

    /**
     * 查询系统配置详细
     * 
     * @param configForm
     */
    public int queryDetailById(ConfigForm configForm) {
        String id = configForm.getSystemConfigDetail().getId();
        if (StringUtil.isNullOrEmpty(id)) {
            return 1;
        }
        SystemConfigDetail systemConfigDetail = systemConfigDetailDao.get(id);
        if (systemConfigDetail == null) {
            return 1;
        }
        configForm.setSystemConfigDetail(systemConfigDetail);
        return 0;
    }

    /**
     * 修改系统配置详细
     * 
     * @param configForm
     * @return
     */
    public ActionResult updateSystemConfigDetail(ConfigForm configForm) {
        ActionResult result = ActionResult.getInstance();
        SystemConfigDetail systemConfigDetail = systemConfigDetailDao
                .get(configForm.getSystemConfigDetail().getId());

        if (systemConfigDetail == null) {
            result.setOk(false);
            result.setMessage("此配置详细不存在");

        } else if (systemConfigDetailDao.getCountByName(configForm
                .getSystemConfigDetail().getName()) > 0) {

            result.setOk(false);
            result.setMessage("相同配置名称类型已存在");
        } else {

            String name = configForm.getSystemConfigDetail().getName();
            systemConfigDetail.setName(name);
            systemConfigDetail.setAllowConfig(configForm
                    .getSystemConfigDetail().isAllowConfig());
            systemConfigDetail.setValue(configForm.getSystemConfigDetail()
                    .getValue());
            systemConfigDetail.setMemo(configForm.getSystemConfigDetail()
                    .getMemo());
            systemConfigDetailDao.update(systemConfigDetail);

            result.setValue(systemConfigDetail.getId());
        }
        return result;
    }

    /**
     * 删除系统配置详细
     * 
     * @param configForm
     * @return
     */
    public ActionResult deleteSystemConfigDetail(ConfigForm configForm) {
        ActionResult result = ActionResult.getInstance();
        String id = configForm.getSystemConfigDetail().getId();
        SystemConfigDetail systemConfigDetail = systemConfigDetailDao
                .get(configForm.getSystemConfigDetail().getId());

        if (systemConfigDetail == null) {
            result.setOk(false);
            result.setMessage("此配置已不存在");
        } else {

            systemConfigDetailDao.delete(id);
        }

        return result;
    }

    public String queryType(ConfigForm configForm) {
        String sql = "SELECT id,name FROM system_config_type WHERE allowConfig=TRUE ORDER BY id";
        return sqlHelper.queryJson(sql);
    }

    public String queryDate(ConfigForm configForm) {
        String id = configForm.getSystemConfigType().getId();
        String sql = "SELECT id,systemConfigTypeId,name,value,memo FROM system_config_detail WHERE systemConfigTypeId IN "
                + "(SELECT id FROM system_config_type WHERE allowConfig=TRUE AND id=?) AND allowConfig=TRUE";
        return sqlHelper.queryJson(sql, id);

    }

    /**
     * 配置准备
     * 
     * @param configForm
     * @return
     */
    public int queryById(ConfigForm configForm) {
        SystemConfigDetail systemConfigDetail = systemConfigDetailDao.get(configForm.getSystemConfigDetail().getId());
        if (systemConfigDetail == null) {
            return 1;
        } else {
            configForm.setSystemConfigDetail(systemConfigDetail);
            return 0;
        }
    }

    public ActionResult update(ConfigForm configForm) {
        ActionResult result = ActionResult.getInstance();
        SystemConfigDetail systemConfigDetail = systemConfigDetailDao
                .get(configForm.getSystemConfigDetail().getId());
        if (systemConfigDetail == null) {
            result.setOk(false);
            result.setMessage("未找到");
            return result;
        }
        // ObjectUtil.SyncObjectsWithFields(systemConfigDetail,configForm.getSystemConfigDetail(),"value");
        systemConfigDetail.setValue(configForm.getSystemConfigDetail().getValue());
        systemConfigDetailDao.update(systemConfigDetail);
        return result;
    }

}
